package com.xiaren.biz;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.xiaren.common.IConstant;
import com.xiaren.common.ObjectConstant;
import com.xiaren.util.ComponentUtil;

import javax.sound.midi.Soundbank;

/**
 * 本来用于jtree的右键点击（出现窗体的，但是现在没用上）
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class WindowFloatEvent extends MouseAdapter {

	static final int MOVE = 10;// 偏移量
	private static WindowFloatEvent window = new WindowFloatEvent();

	public static WindowFloatEvent getInstance() {
		return window;
	}

	private WindowFloatEvent() {
		super();
	}

	int x = 0;
	int y = 0;

	@Override
	public void mousePressed(MouseEvent e) {
		x = e.getX();
		y = e.getY();
		super.mousePressed(e);
	}

	@Override
	public void mouseClicked(MouseEvent e) {	// 点击事件
		if (e.getButton() == MouseEvent.BUTTON1) {			// 鼠标左键表示显示或者不显示界面
			if (ObjectConstant.mainFrame.isVisible()) {
				ObjectConstant.mainFrame.setVisible(false);
			} else {
				FrameBiz.setFrameVisible();
			}
		} else if (e.getButton() == MouseEvent.BUTTON3) {	// 右键表示退出程序
			PublicFunction.exitSys();
		}

	}

	@Override
	public void mouseReleased(MouseEvent e) {	// 鼠标释放

	}

	@Override
	public void mouseExited(MouseEvent e) {		// 鼠标离开图标的时候调用
		System.out.println(e);
		System.out.println("当前x" + (ObjectConstant.titleWindow.getX() + ObjectConstant.titleWindow.getWidth()) + "-- 屏幕总宽度：" + ObjectConstant.screenWidth);

		if (ObjectConstant.titleWindow.getX()
				+ ObjectConstant.titleWindow.getWidth() >= ObjectConstant.screenWidth) {
			ComponentUtil.moveXComponent(ObjectConstant.titleWindow.getX(),
					ObjectConstant.titleWindow.getY(), ObjectConstant.screenWidth
							- MOVE, ObjectConstant.titleWindow.getY(),
					ObjectConstant.titleWindow);
		} else if (ObjectConstant.titleWindow.getX() <= 0) {
			ComponentUtil.moveXComponent(ObjectConstant.titleWindow.getX(),
					ObjectConstant.titleWindow.getY(), MOVE
							- ObjectConstant.titleWindow.getWidth(),
					ObjectConstant.titleWindow.getY(),
					ObjectConstant.titleWindow);
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		ObjectConstant.titleWindow.setLocation(e.getXOnScreen() - x,
				e.getYOnScreen() - y);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if (e.getXOnScreen() >= ObjectConstant.screenWidth - MOVE) {
			ComponentUtil
					.moveXComponent(
							ObjectConstant.titleWindow.getX(),
							ObjectConstant.titleWindow.getY(),
							ObjectConstant.titleWindow.getX()
									- (ObjectConstant.titleWindow.getWidth() - 2 * MOVE),
							ObjectConstant.titleWindow.getY(),
							ObjectConstant.titleWindow);
		} else if (e.getXOnScreen() <= MOVE) {
			ComponentUtil.moveXComponent(ObjectConstant.titleWindow.getX(),
					ObjectConstant.titleWindow.getY(), -MOVE,
					ObjectConstant.titleWindow.getY(),
					ObjectConstant.titleWindow);
		}
	}

}
